package com.lmqb.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lmqb.bean.SysMenu;
import com.lmqb.bean.SysUser;
import com.lmqb.bean.SysUserOperationLog;
import com.lmqb.constant.MsgError;
import com.lmqb.constant.MsgSuccess;
import com.lmqb.entry.MenuTitle;
import com.lmqb.responces.ResultData;
import com.lmqb.service.JiaoYanService;
import com.lmqb.service.SysMenuService;
import com.lmqb.service.SysUserService;
import com.lmqb.util.StrUtil;
@Controller
public class SysMenuController extends BaseController{

	@Autowired
	private SysMenuService sysMenuService;
	
	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private JiaoYanService jiaoYanService;
	
	//跳转到权限管理界面
	@RequestMapping(value="sysMenu.html",method=RequestMethod.GET)
	public String toIndex(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
		if(sysUser==null){
			return "login";
		}
		SysMenu sysMenu=new SysMenu();
		List<SysMenu> list=sysMenuService.selectAll(sysMenu);
		request.setAttribute(AllSysMenuF, list);
		MenuTitle menuTitle=new MenuTitle();
		menuTitle.setParent("系统配置");
		menuTitle.setMenu("权限管理");
		session.setAttribute(menuTitleF, menuTitle);
		return "sysMenuList";
	}
	
	//跳转到添加菜单页面
	@RequestMapping(value="toAddMenu.html",method=RequestMethod.GET)
	public String toAddMeu(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
		if(sysUser==null){
			return "login";
		}
		SysMenu sysMenu=new SysMenu();
		sysMenu.setParentId(0);
		List<SysMenu> list=sysMenuService.selectAll(sysMenu);
		request.setAttribute(AllSysMenuF, list);
		MenuTitle menuTitle=new MenuTitle();
		menuTitle.setParent("系统配置");
		menuTitle.setMenu("添加菜单");
		session.setAttribute(menuTitleF, menuTitle);
		return "addSysMenu";
	}
	
	//添加菜单
	@RequestMapping(value="addMenu",method=RequestMethod.POST)
	@ResponseBody
	public ResultData<String> addMenu(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,String menuCode,String menuName,
			String menuUrl,Integer parentId){
		ResultData<String> resultData=new ResultData<String>();
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				resultData.setNotLogin();
				return resultData;
			}
			SysMenu sysMenu=new SysMenu();
			sysMenu.setMenuCode(menuCode);
			sysMenu.setMenuName(menuName);
			sysMenu.setMenuStatus((byte)1);
			sysMenu.setMenuUrl(menuUrl);
			sysMenu.setMenuSort((byte)0);
			sysMenu.setParentId(parentId);
			resultData=jiaoYanService.addMenu(sysMenu);
			sysMenuService.insert(sysMenu,sysUser);
			
			resultData.setSuccess(MsgSuccess.ChengGong, MsgSuccess.ChengGong);
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
		}
		return resultData;
	}
	
	//跳转到修改菜单页面
	@RequestMapping(value="toModifyMenu.html",method=RequestMethod.GET)
	public String toModifyMenu(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,Integer menuId){
		SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
		if(sysUser==null){
			return "login";
		}
		SysMenu sysMenu=new SysMenu();
		sysMenu.setParentId(0);
		List<SysMenu> list=sysMenuService.selectAll(sysMenu);
		SysMenu menu=sysMenuService.selectById(menuId);
		request.setAttribute(SysMenuF, menu);
		request.setAttribute(AllSysMenuF, list);
		MenuTitle menuTitle=new MenuTitle();
		menuTitle.setParent("系统配置");
		menuTitle.setMenu("修改菜单");
		session.setAttribute(menuTitleF, menuTitle);
		return "modifySysMenu";
	}
	
	//修改菜单
	@RequestMapping(value="modifyMenu",method=RequestMethod.POST)
	@ResponseBody
	public ResultData<String> modifyMenu(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,SysMenu sysMenu){
		ResultData<String> resultData=new ResultData<String>();
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				resultData.setNotLogin();
				return resultData;
			}
			resultData=jiaoYanService.modifyMenu(sysMenu);
			sysMenuService.update(sysMenu);
			//添加管理员操作记录
			SysUserOperationLog log=new SysUserOperationLog();
			log.setAddTime(StrUtil.timeToInt(new Date()));
			log.setLoginName(sysUser.getLoginName());
			log.setObjectId(sysMenu.getMenuId()+"");
			log.setOperationCate((byte)5);
			log.setOperationContent("修改权限。"+sysMenu.toString());
			log.setRealName(sysUser.getRealName());
			log.setUserId(sysUser.getUserId());
			log.setTableName("sys_menu");
			sysUserService.insertLog(log);
			resultData.setSuccess(MsgSuccess.ChengGong, MsgSuccess.ChengGong);
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
		}
		return resultData;
	}
	
	//停用菜单
	@RequestMapping(value="delMenu",method=RequestMethod.POST)
	@ResponseBody
	public ResultData<String> delMenu(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,Integer menuId){
		ResultData<String> resultData=new ResultData<String>();
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				resultData.setNotLogin();
				return resultData;
			}
			if(menuId==null){
				resultData.setError(MsgError.NullMenuId);
				return resultData;
			}
			SysMenu sysMenu1=sysMenuService.selectById(menuId);
			SysMenu sysMenu=new SysMenu();
			sysMenu.setMenuId(menuId);
			sysMenu.setMenuStatus((byte)2);
			sysMenuService.update(sysMenu);
			//添加管理员操作记录
			SysUserOperationLog log=new SysUserOperationLog();
			log.setAddTime(StrUtil.timeToInt(new Date()));
			log.setLoginName(sysUser.getLoginName());
			log.setObjectId(sysMenu.getMenuId()+"");
			log.setOperationCate((byte)5);
			log.setOperationContent("停用权限"+sysMenu1.getMenuName()+",权限ID："+menuId);
			log.setRealName(sysUser.getRealName());
			log.setUserId(sysUser.getUserId());
			log.setTableName("sys_menu");
			sysUserService.insertLog(log);
			resultData.setSuccess(MsgSuccess.ChengGong, MsgSuccess.ChengGong);
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
		}
		return resultData;
	}
	
	//恢复菜单
	@RequestMapping(value="recoveryMenu",method=RequestMethod.POST)
	@ResponseBody
	public ResultData<String> recoveryMenu(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,Integer menuId){
		ResultData<String> resultData=new ResultData<String>();
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				resultData.setNotLogin();
				return resultData;
			}
			if(menuId==null){
				resultData.setError(MsgError.NullMenuId);
				return resultData;
			}
			SysMenu sysMenu1=sysMenuService.selectById(menuId);
			SysMenu sysMenu=new SysMenu();
			sysMenu.setMenuId(menuId);
			sysMenu.setMenuStatus((byte)1);
			sysMenuService.update(sysMenu);
			//添加管理员操作记录
			SysUserOperationLog log=new SysUserOperationLog();
			log.setAddTime(StrUtil.timeToInt(new Date()));
			log.setLoginName(sysUser.getLoginName());
			log.setObjectId(sysMenu.getMenuId()+"");
			log.setOperationCate((byte)5);
			log.setOperationContent("恢复权限"+sysMenu1.getMenuName()+",权限ID："+menuId);
			log.setRealName(sysUser.getRealName());
			log.setUserId(sysUser.getUserId());
			log.setTableName("sys_menu");
			sysUserService.insertLog(log);
			resultData.setSuccess(MsgSuccess.ChengGong, MsgSuccess.ChengGong);
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
		}
		return resultData;
	}
}
